Perl numerical sorting: how to ignore leading alpha character [migrated]

Posted by Luke Sheppard on Programmers See other posts from Programmers or by Luke Sheppard
Published on 2012-10-11T23:07:05Z Indexed on 2012/10/12 3:48 UTC
Read the original article Hit count: 346

Filed under:
|
|

I have a 1,660 row array like this:

...
H00504
H00085
H00181
H00500
H00103
H00007
H00890
H08793
H94316
H00217
...

And the leading character never changes. It is always "H" then five digits. But when I do what I believe is a numerical sort in Perl, I'm getting strange results. Some segments are sorted in order, but then a different segment starts up. Here is a segment after sorting:

...
H01578
H01579
H01580
H01581
H01582
H01583
H01584
H00536
H00537
H00538
H01585
H01586
H01587
H01588
H01589
H01590
...

What I'm trying is this:

my @sorted_array = sort {$a <=> $b} @raw_array;

But obviously it is not working. Anyone know why?

© Programmers or respective owner

Related posts about perl

Related posts about sorting